<?php
namespace Admin\Controller;
use Common\Controller\BackendController;

class BrandCateController extends BackendController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->_mod = D('BrandCate');
    }
    public function index()
    {
        $zym_16 = $this->_request('sort', 'trim', 'ordid');
        $zym_15 = $this->_request('order', 'trim', 'ASC');
        import("ITK.ORG.Tree");
        $zym_14 = new \Tree();
        $zym_14->icon = array('│ ', '├─ ', '└─ ');
        $zym_14->nbsp = '   ';
        $zym_18 = $this->_mod->order($zym_16 . ' ' . $zym_15)->select();
        $zym_19 = array();
        foreach ($zym_18 as $zym_24) {
            $zym_24['str_status'] = '<img data-tdtype="toggle" data-id="' . $zym_24['id'] . '" data-field="status" data-value="' . $zym_24['status'] . '" src="__STATIC__/images/admin/toggle_' . ($zym_24['status'] == 0 ? 'disabled' : 'enabled') . '.gif" />';
            $zym_24['str_manage'] = '<a href="javascript:;" class="J_showdialog" data-uri="' . U('BrandCate/add', array('pid' => $zym_24['id'])) . '" data-title="' . L('add_BrandCate') . '" data-id="add" data-width="500" data-height="360">' . L('add_brand_subcate') . '</a> |
                               <a href="javascript:;" class="J_showdialog" data-uri="' . U('BrandCate/edit', array('id' => $zym_24['id'])) . '" data-title="' . L('edit') . ' - ' . $zym_24['name'] . '" data-id="edit" data-width="500" data-height="290">' . L('edit') . '</a> |
                               <a href="javascript:;" data-acttype="ajax" class="J_confirmurl" data-uri="' . U('BrandCate/delete', array('id' => $zym_24['id'])) . '" data-msg="' . sprintf(L('confirm_delete_one'), $zym_24['name']) . '">' . L('delete') . '</a>';
            $zym_24['parentid_node'] = $zym_24['pid'] ? ' class="child-of-node-' . $zym_24['pid'] . '"' : '';
            $zym_19[] = $zym_24;
        }
        $zym_22 = '<tr id=\'node-$id\' $parentid_node>
               <td align=\'center\'><input type=\'checkbox\' value=\'$id\' class=\'J_checkitem\'></td>
               <td>$spacer<span data-tdtype=\'edit\' data-field=\'name\' data-id=\'$id\' class=\'tdedit\'>$name</span></td>
               <td align=\'center\'>$id</td>
               <td align=\'center\'><span data-tdtype=\'edit\' data-field=\'ordid\' data-id=\'$id\' class=\'tdedit\'>$ordid</span></td>
               <td align=\'center\'>$str_status</td>
               <td align=\'center\'>$str_manage</td>
               </tr>';
        $zym_14->init($zym_19);
        $zym_21 = $zym_14->get_tree(0, $zym_22);
        $this->assign('list', $zym_21);
        $zym_20 = array('title' => L('add_BrandCate'), 'iframe' => U('BrandCate/add'), 'id' => 'add', 'width' => '500', 'height' => '290');
        $this->assign('big_menu', $zym_20);
        $this->assign('list_table', true);
        $this->display();
    }
    public function _before_add()
    {
        $zym_13 = $this->_get('pid', 'intval', 0);
        if ($zym_13) {
            $zym_12 = $this->_mod->where(array('id' => $zym_13))->getField('spid');
            $zym_12 = $zym_12 ? $zym_12 . $zym_13 : $zym_13;
            $this->assign('spid', $zym_12);
        }
    }
    protected function _before_insert($zym_5 = '')
    {
        if ($this->_mod->name_exists($zym_5['name'], $zym_5['pid'])) {
            $this->ajaxReturn(0, L('BrandCate_already_exists'));
        }
        $zym_5['spid'] = $this->_mod->get_spid($zym_5['pid']);
        return $zym_5;
    }
    protected function _before_update($zym_5 = '')
    {
        if ($this->_mod->name_exists($zym_5['name'], $zym_5['pid'], $zym_5['id'])) {
            $this->ajaxReturn(0, L('BrandCate_already_exists'));
        }
        $zym_4 = $this->_mod->field('img,pid')->where(array('id' => $zym_5['id']))->find();
        if ($zym_5['pid'] != $zym_4['pid']) {
            $zym_3 = $this->_mod->get_child_ids($zym_5['id'], true);
            if (in_array($zym_5['pid'], $zym_3)) {
                $this->ajaxReturn(0, L('cannot_move_to_child'));
            }
            $zym_5['spid'] = $this->_mod->get_spid($zym_5['pid']);
        }
        return $zym_5;
    }
    public function ajax_upload_img()
    {
        if (!empty($_FILES['img']['name'])) {
            $zym_18 = $this->_upload($_FILES['img'], 'BrandCate', array('width' => '80', 'height' => '80'));
            if ($zym_18['error']) {
                $this->ajaxReturn(0, $zym_18['info']);
            } else {
                $zym_1 = array_pop(explode('.', $zym_18['info'][0]['savename']));
                $zym_5['img'] = str_replace('.' . $zym_1, '_thumb.' . $zym_1, $zym_18['info'][0]['savename']);
                $this->ajaxReturn(1, L('operation_success'), $zym_5['img']);
            }
        } else {
            $this->ajaxReturn(0, L('illegal_parameters'));
        }
    }
    public function move()
    {
        if (IS_POST) {
            $zym_5['pid'] = $this->_post('pid', 'intval');
            $zym_2 = $this->_post('ids');
            $zym_6 = $this->_mod->where(array('id' => $zym_5['pid']))->getField('spid');
            $zym_7 = explode(',', $zym_2);
            foreach ($zym_7 as $zym_11) {
                if (false !== strpos($zym_6 . $zym_5['pid'] . '|', $zym_11 . '|')) {
                    $this->ajaxReturn(0, L('cannot_move_to_child'));
                }
            }
            $zym_5['spid'] = $this->_mod->get_spid($zym_5['pid']);
            $this->_mod->where(array('id' => array('in', $zym_2)))->save($zym_5);
            $this->ajaxReturn(1, L('operation_success'), '', 'move');
        } else {
            $zym_2 = trim($this->_request('id'), ',');
            $this->assign('ids', $zym_2);
            $zym_10 = $this->fetch();
            $this->ajaxReturn(1, '', $zym_10);
        }
    }
    public function ajax_getchilds()
    {
        $zym_11 = $this->_get('id', 'intval');
        $zym_9 = array('pid' => $zym_11);
        $zym_9['status'] = '1';
        $zym_8 = $this->_mod->field('id,name')->where($zym_9)->select();
        if ($zym_8) {
            $this->ajaxReturn(1, L('operation_success'), $zym_8);
        } else {
            $this->ajaxReturn(0, L('operation_failure'));
        }
    }
}